This video.
I'm going to do some pathfinding.
So I got this guy here.
I named him Sly Dog, and then he's going to find that gold.
There are no pre-programmed wave points.
I'm just going to hit the play 5 seconds after the game starts up.
He's going to start running.
Well, it's going to start now, right?
So we've got to start looking.
There he goes.
And he's finding his way.
He's going to jump that little thing.
And he found the gold.
That's pretty cool.
Pathfinding is a very handy tool.
Let's go ahead and get started with that.
All right.
So I'm in my NPC work still and I deleted Sly Dog and I deleted the treasure so you could build that
with me.
I trust that you could put your own obstacles.
You actually don't even really need it, but it's going to be boring if they don't run around if your
little guy doesn't run around stuff.
So let's go ahead and put our treasure.
Basically, it's a part.
There you go.
And we'll call it treasure.
And then make it like yellow or something like gold and then maybe metal.
Cool.
Got a bar of gold and now we've got to make Sly Dog or any NPC, any name of choice.
I'm going to go to my toolbox home toolbox, and I'm just going to get Ralph and I'm going to rename
them.
And there are some waypoints there.
Oh yeah, we have two scripts.
That's all right.
We could change.
Ralph I probably should just saved them off without the waypoints.
Let's call Ralph something else.
We can call them slaw dog.
Let's eat hot dogs with co slaw.
There we go.
And I'm going to get rid of the thumbnail camera.
Should I save that?
I'm going to get rid of the waypoints.
We're going to generate our own and move NPC.
I'm going to get rid of everything but the char.
Let's define the rest of our variables that we're going to need for pathfinding.
I'm going to start out with the treasure.
Right.
And remember what we have in the workspace, right?
Remember, treasure is just a part and humanoid root parts are just parts.
So if you blend the last video of this video, you could use pathfinding to chase players around and
other non playing characters by using that fine closest HR app or fine closes char that we did in the
last video.
So get a little bit creative and start blending this stuff, but I'm just using a part to keep it simple
for now.
All right.
Now what I'm going to need is the path finding service right game, get service, pathfinding service,
and that's going to allow me to compute my waypoints between me and where I want to go.
In this case, the treasure.
I'm going to need a path, right?
I'm going to get the path from the path finding service path, finding service, colon and create path.
There you go.
It gives you a little explanation.
What else are we going to need?
Oh, we're going to need to keep track of the waypoints we're on when we're running around.
So I'm going to do a waypoint index.
I'm going to initialize it to one.
Right.
And we're also going to need a waypoints table.
We'll just make it empty to store our waypoints when we compute them.
What else am I going to need?
I got my character.
I need the humanoid.
I probably should have deleted it from Ralph, but that's all right.
We'll just redo it.
Chas, wait for child.
I'll do a wait for child humanoid.
And we need the help.
Wait for child humanoid root part.
Let's go down to lines and then define the function.
So local function follow path underpass in the goal.
Right?
Or it could be the HRP if you were if you're chasing somebody.
Right.
That's a part.
The goal is the part.
And I'm going to get a I'm going to use my path variable and then compute a sync, right?
Compute a sync.
And I have to have the start and the stop and this is going to compute my path.
So the start is me, my HPE position, my humanoid route, part position, and then the goal is where
I want to go, but I need that position to and I'm going to re initialize my waypoint, Oh my gosh,
my waypoints table right with this the curly brackets there, because I might call this over and over
again and this might have waypoints in it.
So let's just clear it out before we go to put new ones in.
I'm going to do it if statement and I check my path status.
Right.
So I did the compute and now I want to know the status of that computation.
We're going to compare it against this e nym path status, success to see if we have a success.
Do we have a success?
We'll do loops path colon, get waypoints.
There we go.
And that's only going to happen if there's a success.
If there's a failure, this is going to leave the function and nothing's going to happen.
So you'll have to do some checking if you want to, if you want it to be robust.
Right.
So now I want to get my w p index, make that one.
We're going to start fresh on our waypoint path and then I'll say humanoid move to and I get my PES.
This is very similar to our patrol, but we did this dynamically.
We're going to get our waypoint index dot possession.
All right.
Now, the thing is, if we get here, we need to get to the next position.
If you'll remember, two videos back, we did that, so we had this huge move to finish, right?
And then we connected it to a function, an anonymous function.
I did not do this in the in the simple patrol, but we have available to us a reached flag.
So if the humanoid did make it to this point, this will be true.
If it fell, if it timed out, if 8 seconds went by and didn't get there, this will be false.
So you could do stuff with that, right?
Like if you get stuck.
So I'll say if reached and WP index is greater than.
No.
Is the less that we want to make sure there's more left.
The number of waypoints in our table, then.
Let's go ahead and say WP index plus equals one.
Let's add let's go to the next index for our waypoints and then I need to check between every waypoint
if I need to jump.
So I'm going to do this.
Woops.
WP index dot action equals.
So if the waypoint action equals E waypoint path action dot jump, we need to jump.
So I'll say.
HUME Jump.
So we're adding jumping to this.
True.
All right.
And then.
We need to do to move to the next waypoint.
WP -- WP index dot possession.
Cool.
Now that should work.
The only thing I'm not handling right now is if something dynamically blocks our path.
But we're going to do that.
We're going to do that in the next video.
It's not going to be that hard.
This is the meat of it and we're not going to start over.
We're not going to have to delete anything.
I do need to test this, though, and we don't have any update loops or anything, so we'll just call
it once.
We'll wait 5 seconds and then we'll do a follow path, pass in the treasure and that we should find
it.
So let's go here.
There's our little guy.
We're going to spawn here.
I move this up so we could see it, so we could see the action.
And let's see.
Ah!
Hey, where are you going?
Oh, man.
Oh, you ran around it.
Oh, that's kind of funny.
The AI outsmarted me, but he made it to the treasure, so that's cool.
Well, let's go ahead and move them and just check to see if he can go through the maze.
I want to see him jump.
So let's get him a little closer to entice him to go through the maze.
Maybe like here.
I don't want to run around the other way.
That would be frustrating.
Here we go.
There you go.
Run through the maze.
Andy jumped.
That's cool.
All right.
So the next video will add, We'll have to do a little trigger and then make a door pop up, maybe make
this get bigger or something like that.
And then he has to re compute his waypoint pass to get to his treasure.
So you'll have to run around it then.
That'll be cool.